package cn.itcast.web.action.common;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
     /*
       模型驱动代码优化
      */
    //模型驱动
    protected T model;

    @Override
    public T getModel() {
        return model;
    }

    //构造器实例化model
    public BaseAction() {
        //先获取继承带泛型的父类型对象   extends BaseAction<Area> 再获取<Area>这个泛型
        //获取到继承的父类型泛型
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        //将父类型泛型转为参数化(数组)的数据类型
        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
        //获取第一个泛型的class对象
        Class<T> modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
        try {
            //实例化泛型对象 即为模型对象model
            model = modelClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();

        }
    }
    /*
     分页查询代码优化
     */
    protected  Integer page;
    protected  Integer rows;

    public void setPage(Integer page) {
        this.page = page;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }
}